<template>
    <div class="container">
        <div class="leftMenu">
            <div v-for="item in memuList" :key="item.name" class='menuItem'
                :class="activeMenu === item.name ? 'activeMenu' : ''" @click="scrollToMenu(item)"
                :v-if="item.children?.length > 0">
                <!-- <SvgIcon :svg="item.icon" :size="20" /> -->
                <div class="titleName">{{ item.name }}</div>
            </div>
        </div>
        <div class="chart">
            <div class="imageBigBox">
                <!-- 为每个菜单项的图表区域添加锚点id -->
                <!-- 渲染所有图片 -->
                <div v-for="item in memuList" :key="'images-' + item.name" :id="'images-' + item.name"
                    class="menuImagesSection">
                    <div :id="'anchor-' + item.name" class="menuSection" v-if="item.children?.length > 0">
                        <h3 class="sectionTitle">{{ item.name }}</h3>
                    </div>

                    <div v-for="chartItem in item.children" :key="chartItem.name" class='menuImageItem'>
                        <div v-for="itemData in filterImages(chartItem.children)" :key="itemData" class="imageWrapper"
                            @click="showXiangqing(chartItem)">
                            <img :src="showResult_images(itemData)" @error="setDefaultImage" alt="" />
                        </div>
                        <div class="chartName">{{ chartItem.name }}</div>
                    </div>
                    <!-- 占位元素，用于布局 -->
                    <div class="menuImageItem zhanwei" v-for="i in 9"></div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { BASE_URL_STATIC } from '@/utils/zongIp.js'// 静态资源IP地址
import { getTopJournalList } from '@/api/index.js'
import router from '../../router';
import defaultImage from '@/assets/default.webp';
const setDefaultImage = (e) => {
    e.target.src = defaultImage;
    e.target.onerror = null; // 防止循环报错
}
const activeMenu = ref('柱状图')// 当前激活的菜单
const memuList = ref([])
// 过滤到只剩图片文件夹
const filterImages = (data) => {
    let images;
    data.forEach(item => {
        if (item.name === 'imgs') {
            images = [item.children[0]];
        }
    });
    return images;
}
// 切换菜单（现在改为滚动到对应位置）
const scrollToMenu = (item) => {
    activeMenu.value = item.name
    // 滚动到对应图片区域
    const element = document.getElementById('images-' + item.name)
    if (element) {
        element.scrollIntoView({
            behavior: 'smooth',
            block: 'start'
        })
    }
}

// 显示详情
const showXiangqing = (item) => {
    localStorage.setItem('chartItem', JSON.stringify(item))
    // 跳转详情页并传参
    router.push({ path: 'topJournalDetail' })
}

const showResult_images = (itemData) => {
    // 去掉path路径的第一个.
    const url = itemData.path?.replace(/^\./, '');
    return BASE_URL_STATIC + url;
    // let imageList
    // itemData.forEach(item => {
    //     if(item.name=="imgs"){
    //         imageList=item.children[0]
    //     }
    // })
    // // 判断是否是生产环境
    // const isProduction = process.env.NODE_ENV === 'production';
    // // 如果是生产环境，则使用BASE_URL
    // let baseUrl = isProduction ? BASE_URL : '';
    // return baseUrl + url;
}
onMounted(() => {
    // 获取菜单数据
    getTopJournalList().then((data) => {
        memuList.value = data.children;
        activeMenu.value = memuList.value[0]?.name || '';
    })
});
</script>

<style lang="scss" scoped>
.container {
    width: 100%;
    min-width: 1000px;
    margin: auto;
    height: 100%;
    display: flex;

    .leftMenu {
        width: 200px;
        height: 100%;
        background-color: #f0f0f0;
        overflow-y: auto;

        .menuItem {
            padding: 10px;
            height: 20px;
            color: #5067A2;
            display: flex;
            align-items: center;
            cursor: pointer;

            .titleName {
                margin-left: 8px;
                line-height: 20px;
            }
        }

        .activeMenu {
            background-color: #5470c6;
            color: #fff;
        }
    }

    .chart {
        flex: 1;
        font-size: 13px;
        padding: 0 10px;
        overflow-y: auto;

        .imageBigBox {
            display: block; // 改为块级元素，垂直排列
            width: 100%;
            max-height: calc(100% - 1px);

            .menuSection {
                width: 100%;

                .sectionTitle {
                    // background-color: #f5f5f5;
                    border-bottom: 1px solid #d8d8d8;
                    padding: 8px 12px;
                    margin: 10px 0 5px 0;
                    // border-left: 4px solid #5470c6;
                    font-size: 17px;
                    font-weight: bold;
                    color: #333;
                }
            }

            .menuImagesSection {
                display: flex;
                flex-wrap: wrap;
                width: 100%;

                // 图表区域样式
                .menuImageItem {
                    text-align: center;
                    width: 210px;
                    margin: 10px auto;
                    display: inline-block; // 横向排列
                    vertical-align: top;

                    .menuImageItemSun {
                        height: 100%;
                    }

                    .imageWrapper {
                        background-color: #fff;
                        margin-bottom: 8px;
                        border-radius: 4px;
                        height: 100%;
                        overflow: hidden;
                        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
                        transition: transform 0.2s, box-shadow 0.2s;
                        display: flex;
                        align-items: center;
                        justify-content: center;

                        &:hover {
                            transform: translateY(-2px);
                            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);

                            img {
                                // 最长边适应
                                max-width: 120%;
                                max-height: 120%;
                                object-fit: cover;
                                cursor: pointer;
                            }
                        }

                        img {
                            // 最短边适应
                            max-width: 100%;
                            max-height: 100%;
                            object-fit: cover;
                            cursor: pointer;
                            // 过渡时间
                            transition: all .5s;
                        }

                    }

                    .showMore {
                        margin-bottom: 8px;
                        border-radius: 4px;
                        height: 100%;
                        text-align: center;
                        width: 100%;
                        cursor: pointer;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        font-weight: 700;
                        font-size: 20px;
                        color: #666;
                    }

                    .chartName {
                        font-size: 12px;
                        color: #666;
                        word-break: break-word;
                        padding: 0 4px;
                    }
                }

                // // menuImageItem只显示24个,大于的24的隐藏
                // .menuImageItem:nth-child(n+25) {
                //     display: none;
                // }

                // 超过2000像素
                @media (min-width: 2500px) {
                    .menuImageItem {
                        width: calc(95% / 12); // 约 10%，即 1/10
                        // 高度需要等于宽度
                        aspect-ratio: 1 / 1;

                        // img {
                        //     width: 100%;
                        //     aspect-ratio: 1 / 1;
                        // }
                    }
                }

                // 情况一：屏幕宽度在 1000px ~ 2000px 之间
                // 媒体查询
                @media (min-width: 1500px) and (max-width: 2500px) {
                    .menuImageItem {
                        width: calc(90% / 6); // 约 16.666%，即 1/6
                        // 高度需要等于宽度
                        aspect-ratio: 1 / 1;

                        // img {
                        //     width: 100%;
                        //     aspect-ratio: 1 / 1;
                        // }
                    }
                }

                // 情况二：屏幕宽度在 500px ~ 1000px 之间
                @media (min-width: 500px) and (max-width: 1500px) {
                    .menuImageItem {
                        width: 24%; // 占父容器的 1/4
                        aspect-ratio: 1 / 1;

                        // img {
                        //     width: 100%;
                        //     height: auto; // 保持比例，或设置固定高度
                        // }
                    }
                }

                .zhanwei {
                    height: 0;
                    margin: 0 auto;
                    padding: 0;
                }
            }
        }
    }

    .dialogContent {
        max-height: calc(100vh - 200px);
        overflow-y: auto;

        .titleBox {
            display: flex;
            justify-content: space-between;
            font-size: 16px;
            margin: 10px;
            margin-top: 0;

            .goToDocument {
                color: #133d98;
                font-weight: 700;
                border-bottom: 2px solid #133d98;
                margin-bottom: -2px;
                cursor: pointer;
            }
        }

        .mainResult {
            text-align: center;
            width: calc(100% - 20px);

            .title {
                text-align: left;
                font-size: 16px;
                font-weight: bold;
                padding: 10px;
                border-bottom: 1px solid #ccc;
            }

            .mainResultImg {
                height: 400px;
            }

            img {
                max-width: 100%;
            }
        }
    }
}
</style>